package com.gonsin.i18n.lib.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class TextInflaterFactory2 implements LayoutInflater.Factory2 {

    private LayoutInflater.Factory2 originFactory;

    public TextInflaterFactory2(LayoutInflater.Factory2 originFactory) {
        this.originFactory = originFactory;
    }

    /**
     * 解析字符串
     */
    void parseTextView(Context context, AttributeSet attrs, View view){

        if(!(view instanceof TextView)){
            return;
        }

        for (int i = 0; i < attrs.getAttributeCount(); i++){
            String attrName = attrs.getAttributeName(i);
            String attrValue = attrs.getAttributeValue(i);
            if(attrName.equals("text") || attrName.equals("hint")){
                int id = Integer.parseInt(attrValue.substring(1));
                if(id == 0) continue;

                TextView tv = (TextView)view;
                String originText = context.getString(id);
                CharSequence text = TextManager.getInstance().getText(originText);
                if (text == null) text = originText;
                if (attrName.equals("text")) tv.setText(text);
                else tv.setHint(text);
            }
        }
    }

    @Nullable
    @Override
    public View onCreateView(@Nullable View parent, @NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) {
        View view = null;
        if(originFactory != null){
            view = originFactory.onCreateView(parent, name, context, attrs);
        } else {
            view = createView(parent, name, context, attrs);
        }

        try {
            if(view == null){
                return null;
            }
            parseTextView(context, attrs, view);
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return view;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull String name, @NonNull Context context, @NonNull AttributeSet attrs) {
        return onCreateView(null, name, context, attrs);
    }


    /**
     * 创建默认的view
     */
    private View createView(View parent, String name, Context context, AttributeSet attrs) {
        View view = null;
        try {
            if (-1 == name.indexOf('.')){
                if ("View".equals(name)) {
                    view = LayoutInflater.from(context).createView(name, "android.view.", attrs);
                }
                if ("SurfaceView".equals(name) && view == null) {
                    view = LayoutInflater.from(context).createView(name, "android.view.", attrs);
                }
                if (view == null) {
                    view = LayoutInflater.from(context).createView(name, "android.widget.", attrs);
                }
                if (view == null) {
                    view = LayoutInflater.from(context).createView(name, "android.webkit.", attrs);
                }
            }else {
                view = LayoutInflater.from(context).createView(name, null, attrs);
            }

        } catch (Exception e) {
            view = null;
        }
        return view;
    }
}
